home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n08.arc / COLORSCR.C < prev    next >
C/C++ Source or Header  |  1991-04-05  |  11KB  |  323 lines

  1. /*------------------------------------------------------------------------
  2.    COLORSSCR.C -- View colors (pure, dithered, palette) using scroll bars
  3.                   (c) Charles Petzold, 1990
  4.   ------------------------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8.  
  9. long FAR PASCAL WndProc    (HWND, WORD, WORD, LONG) ;
  10. long FAR PASCAL ScrollProc (HWND, WORD, WORD, LONG) ;
  11.  
  12. FARPROC lpfnOldScr[3] ;
  13. HWND    hwndScrol[3], hwndLabel[3], hwndValue[3], hwndRect ;
  14. short   color[3], nFocus ;
  15.  
  16. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  17.                     LPSTR lpszCmdLine, int nCmdShow)
  18.      {
  19.      static char szAppName[] = "ColorScr" ;
  20.      static char *szColorLabel[] = { "Red", "Green", "Blue" } ;
  21.      FARPROC     lpfnScrollProc ;
  22.      HWND        hwnd ;
  23.      MSG         msg;
  24.      short       n ;
  25.      WNDCLASS    wndclass ;
  26.  
  27.      if (!hPrevInstance)
  28.           {
  29.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  30.           wndclass.lpfnWndProc   = WndProc ;
  31.           wndclass.cbClsExtra    = 0 ;
  32.           wndclass.cbWndExtra    = 0 ;
  33.           wndclass.hInstance     = hInstance ;
  34.           wndclass.hIcon         = NULL ;
  35.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  36.           wndclass.hbrBackground = NULL ;
  37.           wndclass.lpszMenuName  = NULL ;
  38.           wndclass.lpszClassName = szAppName ;
  39.  
  40.           RegisterClass (&wndclass) ;
  41.           }
  42.  
  43.      hwnd = CreateWindow (szAppName, "Color Scroll",
  44.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  45.                           CW_USEDEFAULT, CW_USEDEFAULT,
  46.                           CW_USEDEFAULT, CW_USEDEFAULT,
  47.                           NULL, NULL, hInstance, NULL) ;
  48.  
  49.      hwndRect = CreateWindow ("static", NULL,
  50.                               WS_CHILD | WS_VISIBLE | SS_WHITERECT,
  51.                               0, 0, 0, 0,
  52.                               hwnd, 9, hInstance, NULL) ;
  53.  
  54.      lpfnScrollProc = MakeProcInstance ((FARPROC) ScrollProc, hInstance) ;
  55.  
  56.      for (n = 0 ; n < 3 ; n++) 
  57.           {
  58.           hwndScrol[n] = CreateWindow ("scrollbar", NULL,
  59.                               WS_CHILD | WS_VISIBLE | WS_TABSTOP | SBS_VERT,
  60.                               0, 0, 0, 0,
  61.                               hwnd, n, hInstance, NULL) ;
  62.  
  63.           hwndLabel[n] = CreateWindow ("static", szColorLabel[n],
  64.                               WS_CHILD | WS_VISIBLE | SS_CENTER,
  65.                               0, 0, 0, 0,
  66.                               hwnd, n + 3, hInstance, NULL) ;
  67.  
  68.           hwndValue[n] = CreateWindow ("static", "0",
  69.                               WS_CHILD | WS_VISIBLE | SS_CENTER,
  70.                               0, 0, 0, 0,
  71.                               hwnd, n + 6, hInstance, NULL) ; 
  72.  
  73.           lpfnOldScr[n] = (FARPROC) GetWindowLong (hwndScrol[n], GWL_WNDPROC) ;
  74.           SetWindowLong (hwndScrol[n], GWL_WNDPROC, (LONG) lpfnScrollProc) ;
  75.  
  76.           SetScrollRange (hwndScrol[n], SB_CTL, 0, 255, FALSE) ;
  77.           SetScrollPos   (hwndScrol[n], SB_CTL, 0, FALSE) ;
  78.           }
  79.  
  80.      ShowWindow (hwnd, nCmdShow) ;
  81.      UpdateWindow (hwnd);
  82.  
  83.      while (GetMessage (&msg, NULL, 0, 0))
  84.           {
  85.           TranslateMessage (&msg) ;
  86.           DispatchMessage  (&msg) ;
  87.           }
  88.      return msg.wParam ;
  89.      }
  90.  
  91. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  92.      {
  93.      static HBRUSH     hBrushScr[3] ;
  94.      static HPALETTE   hPal ;
  95.      static LOGPALETTE lp ;
  96.      static short      cxClient, cyClient ;
  97.      char              szbuffer[10] ;
  98.      HBRUSH            hBrush ;
  99.      HDC               hdc ;
  100.      PAINTSTRUCT       ps ;
  101.      POINT             point ;
  102.      RECT              rc ;
  103.      short             n, cyChar ;
  104.      TEXTMETRIC        tm ;
  105.  
  106.      switch (message)
  107.           {
  108.           case WM_CREATE :
  109.  
  110.                     // Initialize the fields of the LOGPALETTE structure
  111.  
  112.                lp.palVersion             = 0x300 ;
  113.                lp.palNumEntries          = 1 ;
  114.                lp.palPalEntry[0].peRed   = 0 ;
  115.                lp.palPalEntry[0].peGreen = 0 ;
  116.                lp.palPalEntry[0].peBlue  = 0 ;
  117.                lp.palPalEntry[0].peFlags = 0 ;
  118.  
  119.                     // Create the logical palette
  120.  
  121.                hPal = CreatePalette (&lp) ;
  122.  
  123.                hBrushScr[0] = CreateSolidBrush (RGB (255, 0, 0)) ;
  124.                hBrushScr[1] = CreateSolidBrush (RGB (0, 255, 0)) ;
  125.                hBrushScr[2] = CreateSolidBrush (RGB (0, 0, 255)) ;
  126.                return 0 ;
  127.  
  128.           case WM_SIZE :
  129.                cxClient = LOWORD (lParam) ;
  130.                cyClient = HIWORD (lParam) ;
  131.  
  132.                hdc = GetDC (hwnd) ;
  133.                GetTextMetrics (hdc, &tm) ;
  134.                cyChar = tm.tmHeight ;
  135.                ReleaseDC (hwnd, hdc) ;
  136.  
  137.                MoveWindow (hwndRect, 0, 0, cxClient / 2, cyClient, TRUE) ;
  138.  
  139.                for (n = 0 ; n < 3 ; n++)
  140.                     {
  141.                     MoveWindow (hwndScrol[n],
  142.                          (2 * n + 1) * cxClient / 14, 2 * cyChar,
  143.                          cxClient / 14, cyClient - 4 * cyChar, TRUE) ;
  144.  
  145.                     MoveWindow (hwndLabel[n],
  146.                          (4 * n + 1) * cxClient / 28, cyChar / 2,
  147.                          cxClient / 7, cyChar, TRUE) ;
  148.  
  149.                     MoveWindow (hwndValue[n],
  150.                          (4 * n + 1) * cxClient / 28, cyClient - 3 * cyChar / 2,
  151.                          cxClient / 7, cyChar, TRUE) ;
  152.                     }
  153.                SetFocus (hwnd) ;
  154.                return 0 ;
  155.  
  156.           case WM_SETFOCUS:
  157.                SetFocus (hwndScrol[nFocus]) ;
  158.                return 0 ;
  159.  
  160.           case WM_VSCROLL :
  161.                n = GetWindowWord (HIWORD (lParam), GWW_ID) ;
  162.  
  163.                switch (wParam)
  164.                     {
  165.                     case SB_PAGEDOWN :
  166.                          color[n] += 15 ;         // fall through
  167.                     case SB_LINEDOWN :
  168.                          color[n] = min (255, color[n] + 1) ;
  169.                          break ;
  170.                     case SB_PAGEUP :
  171.                          color[n] -= 15 ;         // fall through
  172.                     case SB_LINEUP :
  173.                          color[n] = max (0, color[n] - 1) ;
  174.                          break ;
  175.                     case SB_TOP:
  176.                          color[n] = 0 ;
  177.                          break ;
  178.                     case SB_BOTTOM :
  179.                          color[n] = 255 ;
  180.                          break ;
  181.                     case SB_THUMBPOSITION :
  182.                     case SB_THUMBTRACK :
  183.                          color[n] = LOWORD (lParam) ;
  184.                          break ;
  185.                     default :
  186.                          break ;
  187.                     }
  188.  
  189.                SetScrollPos  (hwndScrol[n], SB_CTL, color[n], TRUE) ;
  190.                SetWindowText (hwndValue[n], itoa (color[n], szbuffer, 16)) ;
  191.                InvalidateRect (hwnd, NULL, TRUE) ;
  192.                return 0 ;
  193.  
  194.           case WM_CTLCOLOR:
  195.                if (HIWORD (lParam) == CTLCOLOR_SCROLLBAR)
  196.                     {
  197.                     SetBkColor (wParam, GetSysColor (COLOR_CAPTIONTEXT)) ;
  198.                     SetTextColor (wParam, GetSysColor (COLOR_WINDOWFRAME)) ;
  199.  
  200.                     n = GetWindowWord (LOWORD (lParam), GWW_ID) ;
  201.                     point.x = point.y = 0 ;
  202.                     ClientToScreen (hwnd, &point) ;
  203.                     UnrealizeObject (hBrushScr[n]) ;
  204.                     SetBrushOrg (wParam, point.x, point.y) ;
  205.                     return ((DWORD) hBrushScr[n]) ;
  206.                     }
  207.                break ;
  208.  
  209.           case WM_ERASEBKGND:
  210.                return 1 ;
  211.  
  212.           case WM_PAINT:
  213.                hdc = BeginPaint (hwnd, &ps) ;
  214.  
  215.                rc.left   = cxClient / 2 ;
  216.                rc.right  = cxClient ;
  217.  
  218.                          // Display solid color
  219.  
  220.                rc.top    = 0 ;
  221.                rc.bottom = cyClient / 3 ;
  222.  
  223.                hBrush = CreateSolidBrush (
  224.                              GetNearestColor (hdc,
  225.                                   RGB (color[0], color[1], color[2]))) ;
  226.  
  227.                FillRect (hdc, &rc, hBrush) ;
  228.                DeleteObject (hBrush) ;
  229.  
  230.                          // Display dithered color
  231.  
  232.                rc.top    = rc.bottom ;
  233.                rc.bottom = 2 * cyClient / 3 ;
  234.  
  235.                hBrush = CreateSolidBrush (
  236.                              RGB (color[0], color[1], color[2])) ;
  237.  
  238.                FillRect (hdc, &rc, hBrush) ;
  239.                DeleteObject (hBrush) ;
  240.  
  241.                          // Display palette color
  242.  
  243.                lp.palPalEntry[0].peRed   = color[0] ;
  244.                lp.palPalEntry[0].peGreen = color[1] ;
  245.                lp.palPalEntry[0].peBlue  = color[2] ;
  246.  
  247.                SetPaletteEntries (hPal, 0, 1, lp.palPalEntry) ;
  248.                SelectPalette (hdc, hPal, FALSE) ;
  249.                RealizePalette (hdc) ;
  250.  
  251.                rc.top    = rc.bottom ;
  252.                rc.bottom = cyClient ;
  253.  
  254.                hBrush = CreateSolidBrush (PALETTEINDEX (0)) ;
  255.  
  256.                FillRect (hdc, &rc, hBrush) ;
  257.                DeleteObject (hBrush) ;
  258.  
  259.                EndPaint (hwnd, &ps) ;
  260.                return 0 ;
  261.  
  262.           case WM_QUERYNEWPALETTE:
  263.                hdc = GetDC (hwnd) ;
  264.  
  265.                SelectPalette (hdc, hPal, FALSE) ;
  266.  
  267.                if (RealizePalette (hdc) > 0)
  268.                     {
  269.                     ReleaseDC (hwnd, hdc) ;
  270.                     InvalidateRect (hwnd, NULL, FALSE) ;
  271.                     return TRUE ;
  272.                     }
  273.                else
  274.                     {
  275.                     ReleaseDC (hwnd, hdc) ;
  276.                     return FALSE ;
  277.                     }
  278.                break ;
  279.  
  280.           case WM_PALETTECHANGED:
  281.                if (wParam != hwnd)
  282.                     {
  283.                     hdc = GetDC (hwnd) ;
  284.  
  285.                     SelectPalette (hdc, hPal, FALSE) ;
  286.  
  287.                     if (RealizePalette (hdc) > 0)
  288.                          {
  289.                          InvalidateRect (hwnd, NULL, FALSE) ;
  290.                          }
  291.  
  292.                     ReleaseDC (hwnd, hdc) ;
  293.                     }
  294.                return 0 ;
  295.  
  296.           case WM_DESTROY:
  297.                DeleteObject (hPal) ;
  298.                for (n = 0 ; n < 3 ; DeleteObject (hBrushScr [n++])) ;
  299.                PostQuitMessage (0) ;
  300.                return 0 ;
  301.           }
  302.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  303.      }
  304.  
  305. long FAR PASCAL ScrollProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  306.      {
  307.      short n = GetWindowWord (hwnd, GWW_ID) ;
  308.  
  309.      switch (message)
  310.           {
  311.           case WM_KEYDOWN:
  312.                if (wParam == VK_TAB)
  313.                     SetFocus (hwndScrol[(n +
  314.                          (GetKeyState (VK_SHIFT) < 0 ? 2 : 1)) % 3]) ;
  315.                break ;
  316.  
  317.           case WM_SETFOCUS:
  318.                nFocus = n ;
  319.                break ;
  320.           }
  321.      return CallWindowProc (lpfnOldScr[n], hwnd, message, wParam, lParam) ;
  322.      }
  323.